<?php
include '../system/system.php';
if (isset($_GET["logout"])) {
    setcookie("username", "", time() - 3600 * 72);
    exit("<script>alert('已退出登录');location.href='login.php';</script>");
}
if (isset($_COOKIE["username"])) {
    exit("<script>alert('你已经登录！请勿重复登录');location.href='index.php';</script>");
}
if (isset($_POST["username"]) && isset($_POST["password"])) {
    $user = daddslashes($_POST["username"]);
    $pass = daddslashes($_POST["password"]);
    if ($user == $home["username"] && $pass == $home["password"]) {
        $state = '1';
        $token = $user . '##' . $pass;
        $jm = authcode($token, 'ENCODE', 'codecj80808');
        setcookie("username", $jm, time() + 3600 * 72);
    } else {
        $state = '0';
    }
    die(json_encode(array("state" => $state)));
}
?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title><?php echo $home["title"]; ?> | 管理员登录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="Premium Multipurpose Admin & Dashboard Template" name="description">
    <meta content="Themesdesign" name="author"><!-- App favicon -->
    <link rel="shortcut icon" href="assets/images/favicon.ico"><!-- Bootstrap Css -->
    <link href="../assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css"><!-- Icons Css -->
    <link href="../assets/css/icons.min.css" rel="stylesheet" type="text/css"><!-- App Css-->
    <link href="../assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css">
    <link href="../assets/css/sweetalert2.min.css" rel="stylesheet" type="text/css">
</head>

<body class="bg-pattern">
    <div class="bg-overlay"></div>
    <div class="account-pages my-5 pt-5">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-xl-4 col-lg-6 col-md-8">
                    <div class="card">
                        <div class="card-body p-4">
                            <div class="">
                                <div class="text-center"><a href="index.php" class=""><img src="<?php echo $home["logo"]; ?>" alt="" height="24" class="auth-logo logo-dark mx-auto"></a></div>
                                <!-- end row -->
                                <h4 class="font-size-18 text-muted mt-2 text-center">管理员后台登录</h4>
                                <form class="form-horizontal" onsubmit="return login();">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="mb-4"><label class="form-label" for="username">管理员账号</label><input type="text" class="form-control" id="username" placeholder="请输入的登录账号" required>
                                            </div>
                                            <div class="mb-4"><label class="form-label" for="userpassword">管理员密码</label><input type="password" class="form-control" id="password" placeholder="请输入登录密码" required>
                                            </div>
                                            <div class="d-grid mt-4"><button class="btn btn-primary waves-effect waves-light" type="submit">登录</button></div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="mt-5 text-center">
                        <p class="text-white-50">(c)
                            <script>
                                document.write(new Date().getFullYear())
                            </script> Cloud<i class="mdi mdi-heart text-danger"></i>By <a href="https://www.9gyun.cn">云风轻授权中心</a>
                        </p>
                    </div>
                </div>
            </div><!-- end row -->
        </div>
    </div><!-- end Account pages -->
    <!-- JAVASCRIPT -->
    <script src="../assets/js/jquery.min.js"></script>
    <script src="../assets/js/sweetalert2.min.js"></script>
</body>
<script>
    function login() {
        var user = $("#username").val();
        var pass = $("#password").val();
        $.ajax({
            type: "POST",
            url: "login.php",
            data: {
                username: user,
                password: pass
            },
            dataType: "json",
            success: function(data) {
                if (data.state == 1) {
                    Swal.fire({
                        title: '登录成功!',
                        text: '欢迎回来 ' + user,
                        icon: 'success',
                        confirmButtonText: '进入后台!',
                        confirmButtonClass: 'btn btn-success mt-2',
                    });
                    window.location = 'index.php';
                } else {
                    Swal.fire({
                        title: '登录失败!',
                        text: '账号或密码错误',
                        icon: 'error',
                        confirmButtonText: '重新尝试!',
                        confirmButtonClass: 'btn btn-danger mt-2',
                    });
                }

            },
            error: function(data) {}
        });
        return false;

    }
</script>

</html>